//
//  HomePageWeb.m
//  ZAKER
//
//  Created by dllo on 15/9/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HomePageWeb.h"
#import "WangYiModel.h"
#import "UIImageView+WebCache.h"

@interface HomePageWeb ()<UIWebViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) MBProgressHUD *mb;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, retain) UIScrollView *scrollview;

@end

@implementation HomePageWeb

-(void)dealloc
{
    
    [_scrollview release];
    [_web release];
    [_imageV release];
    
    [_mb release];
    [_dic release];
    [_url release];
    [_deoId release];
    
    [super dealloc];
    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"新闻详情";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
    self.mb = [[[MBProgressHUD alloc] initWithWindow:window] autorelease];
    _mb.labelText = @"正在加载";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
        if (_mb != nil) {
            [_mb removeFromSuperview];
            UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"数据加载失败" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [a addAction:sure];
            // 模态推出
            [self presentViewController:a animated:YES completion:^{
                
            }];
        }
    });
    
    [self.view addSubview:_mb];
    [_mb show:YES];
    
    [MyAFNetworking GetWithURL:self.url dic:nil data:^(id responsder) {
        
        self.dic = responsder[_deoId];
        [self createWebView];
        [_mb hide:YES];
        _mb  = nil;        
    }];
}

- (void)left:(UIBarButtonItem *)left
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



#pragma mark - 创建webView以及实现协议方法
- (void)createWebView
{
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.height - 49)];
    [self.view addSubview:self.scrollview];
    [_scrollview release];
    
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [_imageV sd_setImageWithURL:[NSURL URLWithString:self.model.imgsrc]];
    _imageV.backgroundColor = [UIColor clearColor];
    [self.scrollview addSubview:_imageV];
   
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height - 249)];
    [self.web loadHTMLString:self.dic[@"body"] baseURL:nil];
    [self.scrollview addSubview:self.web];
    self.web.delegate = self;
    [self.web release];
    self.web.scrollView.scrollEnabled = NO;
    self.web.backgroundColor = [UIColor redColor];
    self.web.scrollView.delegate = self;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, 200 + self.web.scrollView.contentSize.height);
    self.web.frame = CGRectMake(0, 200, self.view.frame.size.width, self.web.scrollView.contentSize.height);
    [_mb hide:YES];
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
